package com.ias.agdis_r.fragment.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.BaseFragment;
import com.ias.agdis_r.activity.BoxActivity;
import com.ias.agdis_r.activity.MainActivity;
import com.ias.agdis_r.activity.mine.ApplyActivity;
import com.ias.agdis_r.activity.mine.FlightSubscribeActivity;
import com.ias.agdis_r.activity.task.TaskInfoActivity;
import com.ias.agdis_r.bean.auth.AuthEnum;
import com.ias.agdis_r.manager.DataBaseManager;
import com.ias.agdis_r.manager.DataManager;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.manager.JavaBeanManager;
import com.ias.agdis_r.manager.ServiceManage;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.terminalmanager.restful.base.endpoint.response.BaseResponse;
import com.ias.agdis_r.util.NFCUtil;
import com.ias.agdis_r.util.S;

public class Mine extends BaseFragment {

	private RelativeLayout item_1, item_2, item_4;
	private RelativeLayout ll_flight, ll_no_task, ll_all_task, ll_message;
	private TextView txt_name,txt_position;
	private ImageView iv_avater,iv_bindCar;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_mine, container, false);
		init(view);
		return view;
	}

	private void init(View view) {
		item_1 = (RelativeLayout) view.findViewById(R.id.item_1);
		item_2 = (RelativeLayout) view.findViewById(R.id.item_2);
		item_4 = (RelativeLayout) view.findViewById(R.id.item_4);
		ll_all_task = (RelativeLayout) view.findViewById(R.id.ll_all_task);
		ll_no_task = (RelativeLayout) view.findViewById(R.id.ll_no_task);
		ll_message = (RelativeLayout) view.findViewById(R.id.ll_message);
		txt_name = (TextView) view.findViewById(R.id.txt_name);
		txt_position = (TextView) view.findViewById(R.id.txt_position);
		iv_avater = (ImageView) view.findViewById(R.id.avater);
		iv_bindCar = (ImageView) view.findViewById(R.id.bindCarNumber);
		iv_bindCar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				NFCUtil.showBindConfirm(getActivity(), null);
			}
		});

		updateUI("");
		ll_all_task.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (checkLogin()) {
					Intent mIntent = new Intent(MainActivity.ACTION_TASK);
					getActivity().sendBroadcast(mIntent);
				}
			}
		});
		ll_no_task.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				String mytnb = JavaBeanManager.getInstance().getUserinfo().getMytnb();
				if (!checkLogin()) {

				} else if (S.isEmpty(mytnb)) {
					EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, DataBaseManager.getInstance().getContext().getString(R.string.noTaskDoing), false);
				} else if (null != DataManager.getInstance().getHistoryWorkTask(mytnb)) {
					EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, DataBaseManager.getInstance().getContext().getString(R.string.noTaskDoing), false);
				} else {
					Intent intent = new Intent(getActivity().getApplicationContext(), TaskInfoActivity.class);
					intent.putExtra(MessageModel.TNB, mytnb);
					startActivity(intent);
				}
			}
		});
		ll_message.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (checkLogin()) {
					startActivity(new Intent(getActivity(), BoxActivity.class));
				}
			}
		});
		item_1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(getActivity().getApplicationContext(), ApplyActivity.class));
			}
		});

		item_2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (checkLogin()) {
					Intent mIntent = new Intent(MainActivity.ACTION_MSG);
					getActivity().sendBroadcast(mIntent);
				}
			}
		});

		item_4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent mIntent = new Intent(MainActivity.ACTION_MORE);
				getActivity().sendBroadcast(mIntent);
			}
		});
		ll_flight = (RelativeLayout) view.findViewById(R.id.ll_flight);
		ll_flight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checkLogin()) {
					//如果有订阅权限
					if (JavaBeanManager.getInstance().adjust_pri(AuthEnum.AI_T_FLIGHTSUB.toString())) {
						startActivity(new Intent(getActivity().getApplicationContext(), FlightSubscribeActivity.class));
					} else {
						EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_UPTO, DataBaseManager.getInstance().getContext().getString(R.string.no_AI_T_FLIGHTSUB), false);
					}
				}
			}
		});
		view.findViewById(R.id.layout_user_info).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				checkLogin();
			}
		});

	}

	@Override
	protected void operateResponseSucess(String serviceId, BaseResponse response) {
		if (MessageModel.UCBD.equals(serviceId)){
			String vnb = JavaBeanManager.getInstance().getUserinfo().getVnb();
			if (txt_position != null && S.isNotEmpty(vnb)){
				txt_position.setText(JavaBeanManager.getInstance().getUserinfo().getRln()+"("+vnb+")");
			}
		}
	}

	@Override
	protected void updateUI(String tag) {
		// TODO Auto-generated method stub
		String userName = getString(R.string.unlogin);
		txt_position.setVisibility(View.GONE);
		int id = R.drawable.user_offline;
		iv_bindCar.setVisibility(View.GONE);
		if (ServiceManage.getInstance().getIsLogon()) {
			int bindCarIcon = R.drawable.unbindcar;
			userName = JavaBeanManager.getInstance().getUserinfo().getEnm();
			if("admin".equals(userName)){
				txt_position.setText(getString(R.string.DM_1));
			}else {
				String vnb = JavaBeanManager.getInstance().getUserinfo().getVnb();
				if (S.isNotEmpty(vnb)){
					bindCarIcon = R.drawable.bindcar;
					txt_position.setText(JavaBeanManager.getInstance().getUserinfo().getRln()+"("+vnb+")");
				}else {
					txt_position.setText(JavaBeanManager.getInstance().getUserinfo().getRln());
				}
			}
			txt_position.setVisibility(View.VISIBLE);
            id = R.drawable.user_online;
			if (JavaBeanManager.getInstance().adjust_pri(AuthEnum.AI_T_USERCARBIND.toString())){
				iv_bindCar.setVisibility(View.VISIBLE);
				iv_bindCar.setBackgroundResource(bindCarIcon);
			}
		}
		txt_name.setText(userName);
		iv_avater.setImageResource(id);
		if (MessageModel.UCUB.equals(tag)){
			String vnb = JavaBeanManager.getInstance().getUserinfo().getVnb();
			if (txt_position != null && S.isNotEmpty(vnb)){
				txt_position.setText(JavaBeanManager.getInstance().getUserinfo().getRln()+"("+vnb+")");
			}
		}
	}

	@Override
	protected void updateUI(String tag, String tnb) {

	}

	/**
	 * 判断是否登录，如果未登录，跳转到登录页面
	 * @return
	 */
	public boolean checkLogin()
	{
		if (!ServiceManage.getInstance().getIsLogon()) {
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE,getString(R.string.use_function_tologin), false);
			ServiceManage.getInstance().startLoginActivity();
			return false;
		}
		return true;
	}
}
